// JavaScript Document
/*
File - master js file
Version - 1.0
*/


//total page load time
var startTime = new Date();
startTime = startTime.getTime();

//IP address
/*if (java && java.net)
	ip = ''+java.net.InetAddress.getLocalHost().getHostAddress();
else 
	ip = 'unknown';
*/
ip = '';

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
/*alert("Browser name: "+BrowserDetect.browser);
alert("Browser version: "+BrowserDetect.version);
alert("OS name: "+BrowserDetect.OS);*/
//------------------------------------------------------------------

/* cookie construct start */
function Cookie(document,name,hours,path,domain,secure) {
  // any VAR in "this" that does not start with a "$" will
  // be written into the cookie (read from also)
  this.$doc  = document
  this.$name = name
  if (hours)  this.$expiration=new Date((new Date()).getTime()+hours*3600000); else this.$expiration = null
  if (path)   this.$path   = path;                                             else this.$path       = null
  if (domain) this.$domain = domain;                                           else this.$domain     = null
  if (secure) this.$secure = true;                                             else this.$secure     = false
}

function CookieWrite() {
  var cookieval=""
  for(var prop in this) {
    if ((prop.charAt(0) == '$') || ((typeof this[prop]) == 'function') || prop == '') continue
	if (cookieval != "") cookieval += '&'
	cookieval+=prop+":"+escape(this[prop])
  }
  var cookie=this.$name+"="+cookieval
  if (this.$expiration) cookie+='; expires=' + this.$expiration.toGMTString()
  if (this.$path)       cookie+='; path='    + this.$path
  if (this.$domain)     cookie+='; domain='  + this.$domain
  if (this.$secure)     cookie+='; secure'
  this.$doc.cookie=cookie
}

function CookieRead() {
  var allcookies=this.$doc.cookie
  if (allcookies=="") {
    return false
  }
  var start= allcookies.indexOf(this.$name+'=')
  if (start== -1) {
    return false
  }
  start += this.$name.length+1
  var end=allcookies.indexOf(';',start)
  if (end == -1) end=allcookies.length
  var cookieval = allcookies.substring(start,end)
  var a = cookieval.split('&')
  for (var i=0;i < a.length;i++) a[i]=a[i].split(':')
  for (var i=0;i < a.length;i++) this[a[i][0]]=unescape(a[i][1])
  return true
}

function CookieDelete() {
  var cookie = this.$name+'='
  if (this.$path)   cookie+='; path='+this.$path
  if (this.$domain) cookie+='; domain='+this.$domain
  cookie+='; expires=Fri, 02-Jan-1970 00:00:00 GMT'  // MAKE IT EXPIRE!
  this.$doc.cookie=cookie
}

new Cookie()
Cookie.prototype.write = CookieWrite
Cookie.prototype.del   = CookieDelete
Cookie.prototype.read  = CookieRead
/* cookie construct end */

var set_cname = location.pathname;
set_cname = set_cname.replace(/\//gi, "_");
set_cname = set_cname.replace(/\./gi, "_");

var myCookie=new Cookie(document,set_cname,10000);
    myCookie.read();
var count=myCookie.count;
if(count!=parseInt(count))count=0;
var FIRST_VISIT=(!count)?true:false;
count++;
myCookie.count=count;
myCookie.write();

//------------------------------------------------------------------


var stopTime = new Date();
stopTime = stopTime.getTime();
var total_page_load_time = ((stopTime - startTime) / 1000);//" seconds"

var viewportwidth;
var viewportheight; 
// the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight 
 if (typeof window.innerWidth != 'undefined')
 {
      viewportwidth = window.innerWidth,
      viewportheight = window.innerHeight
 } 
// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
 else if (typeof document.documentElement != 'undefined'
     && typeof document.documentElement.clientWidth !=
     'undefined' && document.documentElement.clientWidth != 0)
 {
       viewportwidth = document.documentElement.clientWidth,
       viewportheight = document.documentElement.clientHeight
 }
 // older versions of IE 
 else
 {
       viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
       viewportheight = document.getElementsByTagName('body')[0].clientHeight
 }

var winW = 630, winH = 460;
if (parseInt(navigator.appVersion)>3) {
 if (navigator.appName=="Netscape") {
  winW = window.innerWidth;
  winH = window.innerHeight;
 }
 if (navigator.appName.indexOf("Microsoft")!=-1) {
  winW = document.body.offsetWidth;
  winH = document.body.offsetHeight;
 }
}

var site_details = { 	
	"br_code_name"    	:   navigator.appCodeName, 
	"br_name"       	:   BrowserDetect.browser, 
	"br_version" 		:   BrowserDetect.version,
	"br_cookie_enb" 	:   navigator.cookieEnabled,
	"br_platform" 		:   navigator.platform,
	"br_userAgent" 		:   navigator.userAgent,
	"br_language" 		: 	navigator.language,
	"br_product" 		: 	navigator.product,
	"br_onLine" 		: 	navigator.onLine,
	"br_platform" 		: 	navigator.platform,
	"br_vendor" 		: 	navigator.vendor,
	"br_product_sub" 	: 	navigator.productSub,
	"br_build_ID" 		: 	navigator.buildID,
	"br_oscpu" 			: 	BrowserDetect.OS,
	"br_cpu_class" 		: 	navigator.cpuClass,	
	"sc_top" 			: 	window.screen.top,
	"sc_height" 		: 	winH,
	"sc_width" 		: 	winW,
	"sc_left" 		: 	window.screen.left,
	"sc_pixelDepth" 	: 	window.screen.pixelDepth,
	"sc_colorDepth" 	: 	window.screen.colorDepth,
	"sc_availWidth" 	: 	viewportwidth,
	"sc_availHeight" 	: 	viewportheight,
	"sc_availLeft" 		: 	window.screen.availLeft,
	"sc_availTop" 		: 	window.screen.availTop,	
	"hs_length" 		: 	history.length,
	"doc_ref" 			: 	document.referrer,
	"ip_add"			: 	ip,
	"location_href"		:	location.href,
	"location_protocol"	:	location.protocol,
	"location_host"		:	location.host,
	"location_pathname"	:	location.pathname,
	"page_visits"		:	count
};

function doajx(url,params){	
	var http;  // The variable that makes Ajax possible!
	try{// Opera 8.0+, Firefox, Safari
	http = new XMLHttpRequest();}
	catch (e){// Internet Explorer Browsers
	try{
	http = new ActiveXObject("Msxml2.XMLHTTP");}
	catch (e){
	try{
	http = new ActiveXObject("Microsoft.XMLHTTP");}
	catch (e){// Something went wrong
	return false;}}}

	http.open("POST", url, true);	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");	
	http.onreadystatechange = function() {//Call a function when the state changes.
		if(http.readyState == 4 && http.status == 200) {
			//alert("success..");
			//alert(http.responseText);
		}
	}
	http.send(params);
}

var params = '';
params += "aw_code="+aw_code;
for (x in site_details)
{
	params += '&'+x+'='+site_details[x];
}
var remote_url =  aw_site_url+'ax_add.php';	
//doajx(remote_url,params);	

function fnonload(){
	var stopTime = new Date();
	stopTime = stopTime.getTime();
	var total_page_load_time = ((stopTime - startTime) / 1000);//" seconds"
	params += "&total_page_load_time="	+total_page_load_time;	
	doajx(remote_url,params);
	return true;
}

/*var html = "";
for (var n in site_details)
	html += "<tr><td>site_details." + n + "<\/td><td>" + site_details[n] + "&nbsp\;<\/td><\/tr>";

document.write("<table class='bord' border='1' cellpadding='4' cellspacing='0' width='100%'>");
document.write("<tr><th>Property<\/th><th>Value<\/th><\/tr>");
document.write(html);
document.write("<\/table>");
document.write("<br><\/div>");*/

function fnunload(){
	var endTime = new Date();
	endTime = endTime.getTime();
	var total_time = ((endTime - startTime) / 1000);//" seconds"
	
	var site_details = {
	"location_href"		:	location.href,
	"location_pathname"	:	location.pathname,
	"total_time"		:	total_time
	};
	
 	var params = '';
	params += "aw_code="+aw_code;
	for (x in site_details)
	{
		params += '&'+x+'='+site_details[x];
	}
	//alert(params);
	var remote_url =  aw_site_url+'ax_time.php';	
	doajx(remote_url,params);
	return true;
}
window.onunload = fnunload;